package com.tydic.controller;


import com.tydic.trans.beans.domain.NotFoundException;
import com.tydic.trans.beans.domain.User;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.MediaType.APPLICATION_XML_VALUE;
import static org.springframework.web.bind.annotation.RequestMethod.*;

@Controller
@RequestMapping(value = "/users", produces = {APPLICATION_JSON_VALUE, APPLICATION_XML_VALUE, "application/x-smile"})
@Api(value = "/users", tags = "users", description = "关于users的操作")
public class UserController {

    @RequestMapping(value = "/{userId}", method = GET)
    @ApiOperation(
            value = "通过ID查找用户", notes = "当ID <10时，返回用户。ID> 10或非整数将模拟API" +
            "错误条件",
            response = User.class,
            authorizations = {
                    @Authorization(value = "api_key"),
                    @Authorization(value = "userstore_auth", scopes = {
                            @AuthorizationScope(scope = "write_users", description = ""),
                            @AuthorizationScope(scope = "read_users", description = "")
                    })})
    @ApiResponses(value = {
            @ApiResponse(code = 400, message = "提供的ID无效"),
            @ApiResponse(code = 404, message = "user没找到")}
    )
    public ResponseEntity<User> getuserById(
            @ApiParam(value = "获取用户ID", allowableValues = "range[1,5]", required = true)
            @PathVariable("userId") String userId)
            throws Exception, NotFoundException {
        User user = new User();
        if (null != user) {
            return ResponseEntity.ok(user);
        } else {
            throw new NotFoundException(404, "user not found");
        }
    }

    @RequestMapping(method = POST)
    @ApiOperation(value = "添加一个新用户到商店")
    @ApiResponses(value = {@ApiResponse(code = 405, message = "Invalid input")})
    public ResponseEntity<String> adduser(
            @ApiParam(value = "需要添加到商店的用户对象", required = true) @RequestBody User user) {
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(method = PUT)
    @ApiOperation(value = "更新一个已存在的用户信息",
            authorizations = @Authorization(value = "userstore_auth", scopes = {
                    @AuthorizationScope(scope = "write_users", description = ""),
                    @AuthorizationScope(scope = "read_users", description = "")
            }))
    @ApiResponses(value = {@ApiResponse(code = 400, message = "无效ID"),
            @ApiResponse(code = 404, message = "用户没找到"),
            @ApiResponse(code = 405, message = "验证错误")})
    public ResponseEntity<String> updateuser(
            @ApiParam(value = "需要添加到商店的用户对象", required = true) @RequestBody User user) {
        return ResponseEntity.ok("SUCCESS");
    }

    @RequestMapping(value = "/findByStatus", method = GET)
    @ApiOperation(
            value = "通过状态查询用户",
            notes = "多个状态值可以用逗号分隔的字符串提供",
            response = User.class,
            responseContainer = "List",
            authorizations = @Authorization(value = "userstore_auth", scopes = {
                    @AuthorizationScope(scope = "write_users", description = ""),
                    @AuthorizationScope(scope = "read_users", description = "")
            }))
    @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid status value")})
    public ResponseEntity<List<User>> findusersByStatus(
            @ApiParam(value = "Status values that need to be considered for filter",
                    required = true,
                    defaultValue = "available",
                    allowableValues = "available,pending,sold",
                    allowMultiple = true)
            @RequestParam("status") String status) {
        List<User> users = new ArrayList<>();
        return ResponseEntity.ok(users);
    }

    @RequestMapping(value = "/findByTags", method = GET)
    @ApiOperation(
            value = "通过标签查询用户",
            notes = "多个标签可以用逗号分隔的字符串提供。 使用tag1，tag2，tag3进行测试。",
            response = User.class,
            responseContainer = "List",
            authorizations = @Authorization(value = "userstore_auth", scopes = {
                    @AuthorizationScope(scope = "write_users", description = ""),
                    @AuthorizationScope(scope = "read_users", description = "")
            }))
    @ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid tag value")})
    @Deprecated
    public ResponseEntity<List<User>> findusersByTags(
            @ApiParam(
                    value = "Tags to filter by",
                    required = true,
                    allowMultiple = true)
            @RequestParam("tags") String tags) {
        List<User> users = new ArrayList<>();
        return ResponseEntity.ok(users);
    }

}

